// -----------------------------------------------------------------------
// <copyright file="NodeWorkflow.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using AdCom.Utility;
    using VDC.COMMON;
    
    /// <summary>
    /// Class NodeWorkflow
    /// </summary>
    public class NodeWorkflow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NodeWorkflow" /> class
        /// </summary>
        public NodeWorkflow()
        {
            this.NodeWorkflowId = 0;
            this.WorkflowId = 0;
            this.NodeId = 0;
            this.ConditionsId = 0;
            this.NextNodeId = 0;
            this.ProfileTypeProgressId = 0;
            this.ProfileStatusId = 0;
        }

        /// <summary>
        /// Gets or sets NodeWorkflowId
        /// </summary>
        public int NodeWorkflowId { get; set; }
        
        /// <summary>
        /// Gets or sets WorkflowId
        /// </summary>
        public int WorkflowId { get; set; }
        
        /// <summary>
        /// Gets or sets NodeId
        /// </summary>
        public int NodeId { get; set; }
        
        /// <summary>
        /// Gets or sets ConditionsId
        /// </summary>
        public int ConditionsId { get; set; }
        
        /// <summary>
        /// Gets or sets NextNodeId
        /// </summary>
        public int NextNodeId { get; set; }
                
        /// <summary>
        /// Gets or sets ProfileTypeProgressId
        /// </summary>
        public int ProfileTypeProgressId { get; set; }
        
        /// <summary>
        /// Gets or sets ProfileStatusId
        /// </summary>
        public int ProfileStatusId { get; set; }

        /// <summary>
        /// Gets Workflow
        /// </summary>
        public Workflow Workflow
        {
            get
            {
                try
                {
                    Workflow workflow = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneWorkflowGetById, this.WorkflowId));

                    while (reader.Read())
                    {
                        workflow = new Workflow();

                        if (!reader.IsDBNull(0))
                        {
                            workflow.WorkflowId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            workflow.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            workflow.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            workflow.CreateDate = reader.GetDateTime(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            workflow.Descriptions = reader.GetString(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            workflow.Active = reader.GetBoolean(5);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return workflow;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Node
        /// </summary>
        public Node Node
        {
            get
            {
                try
                {
                    Node node = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeGetById, this.NodeId));

                    while (reader.Read())
                    {
                        node = new Node();

                        if (!reader.IsDBNull(0))
                        {
                            node.NodeId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            node.NodeTypeId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            node.Code = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            node.Name = reader.GetString(3);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return node;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Condition
        /// </summary>
        public Condition Condition
        {
            get
            {
                try
                {
                    Condition condition = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneConditionsGetById, this.ConditionsId));

                    while (reader.Read())
                    {
                        condition = new Condition();

                        if (!reader.IsDBNull(0))
                        {
                            condition.ConditionsId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            condition.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            condition.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            condition.Descriptions = reader.GetString(3);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return condition;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
        
        /// <summary>
        /// Gets NextNode
        /// </summary>
        public Node NextNode
        {
            get
            {
                try
                {
                    Node node = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeGetById, this.NextNodeId));

                    while (reader.Read())
                    {
                        node = new Node();

                        if (!reader.IsDBNull(0))
                        {
                            node.NodeId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            node.NodeTypeId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            node.Code = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            node.Name = reader.GetString(3);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return node;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileTypeProgress
        /// </summary>
        public ProfileTypeProgress ProfileTypeProgress
        {
            get
            {
                try
                {
                    ProfileTypeProgress profileTypeProgress = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileTypeProgressGetById, this.ProfileTypeProgressId));

                    while (reader.Read())
                    {
                        profileTypeProgress = new ProfileTypeProgress();

                        if (!reader.IsDBNull(0))
                        {
                            profileTypeProgress.ProfileTypeProgressId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileTypeProgress.ProgressId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profileTypeProgress.DecideDays = reader.GetInt32(2);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return profileTypeProgress;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets ProfileStatus
        /// </summary>
        public ProfileStatus ProfileStatus
        {
            get
            {
                try
                {
                    ProfileStatus profileStatus = null;
                    SqlDataReader reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneProfileStatusGetById, this.ProfileStatusId));

                    while (reader.Read())
                    {
                        profileStatus = new ProfileStatus();

                        if (!reader.IsDBNull(0))
                        {
                            profileStatus.ProfileStatusId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            profileStatus.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            profileStatus.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            profileStatus.Orders = reader.GetInt16(3);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return profileStatus;
                }
                catch (SqlException ex)
                {
                    LogHelper.Instance.WriteError(ex);
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets NodeWorkflowRolls
        /// </summary>
        public IList<NodeWorkflowRoll> NodeWorkflowRolls
        {
            get
            {
                try
                {
                    IList<NodeWorkflowRoll> lstNodeWorkflowRolls = new List<NodeWorkflowRoll>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeWorkflowRollGetByNodeWorkflowId, this.NodeWorkflowId));
                    while (reader.Read())
                    {
                        NodeWorkflowRoll nodeWorkflowRoll = new NodeWorkflowRoll();
                        if (!reader.IsDBNull(0))
                        {
                            nodeWorkflowRoll.NodeWorkflowRollId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            nodeWorkflowRoll.NodeWorkflowId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            nodeWorkflowRoll.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            nodeWorkflowRoll.RoleName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            nodeWorkflowRoll.ToCc = reader.GetBoolean(4);
                        }

                        lstNodeWorkflowRolls.Add(nodeWorkflowRoll);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstNodeWorkflowRolls;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets NodeWfDefaults
        /// </summary>
        public IList<NodeWfDefault> NodeWfDefaults
        {
            get
            {
                try
                {
                    IList<NodeWfDefault> lstNodeWorkflowRolls = new List<NodeWfDefault>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneNodeWfDefaultGetByNodeWorkflowId, this.NodeWorkflowId));
                    while (reader.Read())
                    {
                        NodeWfDefault nodeWfDefault = new NodeWfDefault();
                        if (!reader.IsDBNull(0))
                        {
                            nodeWfDefault.NodeWfDefaultId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            nodeWfDefault.NodeWorkflowId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            nodeWfDefault.ProfileTypeId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            nodeWfDefault.DepartmentId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            nodeWfDefault.UserId = reader.GetInt32(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            nodeWfDefault.PositionsId = reader.GetInt32(5);
                        }

                        lstNodeWorkflowRolls.Add(nodeWfDefault);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstNodeWorkflowRolls;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
